import com.sun.xml.internal.ws.api.model.wsdl.WSDLOutput;
import org.w3c.dom.ls.LSOutput;

import java.sql.SQLOutput;
import java.util.Arrays;


public class Test {
    /**
     * 判断数组是否有序
     */
    public static void main15(String[] args) {
        int[] array = {1,3,2,5};
        System.out.println(sort(array));
    }

    public static boolean sort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] > array[i+1]) {
                return false;
            }
        }
        return true;
    }

    /**
     * 冒泡排序
     */
    public static void main14(String[] args) {
        int[] array = {21,2,7,32,7};
        bubbleSort(array);
        System.out.println(Arrays.toString(array));
    }
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length-1; i++) {
            for (int j = 0; j < array.length-1-i; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                }
            }
        }
    }


    /**
     * 假设数组有序，利用二分查找指定元素
     */
    public static void main13(String[] args) {
        int[] array = {1,4,7,9,13,16,26,37};
        System.out.println(binarySerch(array, 26));
    }

    public static int binarySerch(int[] array, int key) {
        int i = 0;
        int j = array.length-1;
        while(i <= j) {
            int mid = (i + j) >>> 1;
            if (array[mid] == key) {
                return mid;
            } else if (array[mid] > key) {
                j = mid - 1;
            }else {
                i = mid+1;

        }
        }
        return  -1;
    }

    /**
     * 从数组中找一个数，找到为ture，没找到false
     * @param array
     * @param key
     * @return
     */
    public static boolean findNum(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return true;
            }
        }
        return false;
    }
    public static void main12(String[] args) {
        int[] array = {12,34,13,21,53};
        System.out.println(findNum(array,34));
    }

    /**
     * 求数组和平均值
     */
    public static void main11(String[] args) {
        int[] array = {1,3,4,8,13,25,54};
        System.out.println(avg(array));
    }

    public static double avg(int[] array) {
        double sum = 0;
        double ret = 0;
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        ret = sum / array.length;
        return ret;
    }


    /**
     * 找最大值
     * @param args
     */
    public static void main10(String[] args) {
        int[] array = {1,45,65,78};
        System.out.println(maxarray(array));

    }
    public static int maxarray(int[] array) {
        int max = array[0];
        for (int i = 1; i < array.length; i++) {
            if (array[i] > max){
                max = array[i];
            }
        }
        return max;
    }

    public static void main9(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = Arrays.copyOfRange(array,1,3);
        System.out.println(Arrays.toString(ret));
    } // 打印数组下表1 ，2位，  [1,3）左闭右开

    public static void main8(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = Arrays.copyOf(array,array.length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
        System.out.println("=======================");
        ret[0] = 99;
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(ret));
    }

    public static void main7(String[] args) {
        int[] array = {1,2,3,4,5};
        //就是产生当前对象的一个副本
        //clone这个方法是Object的方法-》类和对象之后
        int[] ret = array.clone();
        System.out.println(Arrays.toString(ret));
    }

    /**
     * public static native void arraycopy(Object src,int  srcPos, Object dest, int destPos,int length);
     *         src:原来的数组
     *         srcPos：从原来的数组这个位置开始拷贝
     *         dest：拷贝后的数组
     *        destPos：拷贝的数组的开始位置
     *       length：拷贝的长度
     * @param args
     */
    public static void main6(String[]args){
        int[] array = {1,2,3,4,5};
        int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.toString(copy));
    }

    /**
     * 数组的拷贝
     * Arrays.copyOf(int[] original, int newLength)
     * original:要拷贝的数组
     * newLength：拷贝后新的长度
     */
        public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = Arrays.copyOf(array,array.length);
        System.out.println(Arrays.toString(ret));
    }
    /**
     * 数组拷贝
     */
    public static void main3(String[] args) {
        int[] array = {1,2,3,4,5};
        int[] ret = copyArray(array);
        System.out.println(Arrays.toString(ret));
    }

    private static int[] copyArray(int[] array) {
        int[] tmp = new int[array.length];
        for(int i = 0; i < array.length; i++) {
            tmp[i] = array[i];
        }
        return tmp;
    }


    /**
     * 数组转字符串
     * @param array
     */
    public static String myToString(int[] array) {
        String ret = "[";
        for (int i = 0; i < array.length; i ++) {
            ret += array[i];
            if(i != array.length-1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main2(String[] args) {
        int[] array = {1,2,3};
        System.out.println(myToString(array));
    }

    public static void main1(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < 100; i++) {
            array[i] = i+1;
            System.out.println(array[i]);
        }
    }
}
